<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">产品数据库更新</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新建</a-button>
          <!-- <a-button type="primary" @click="importFile">导入</a-button> -->

          <UploadFile labelName="导入" :alwaysShowBtn="true" :uploadApi="postSysDeviceImportData" accept="xls,xlsx" :showFileList="false"  @getResultData="importData" />

          <a-button type="primary" @click="downFile">下载导入模板</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="postSysDevicePage"  -->
        <BasicTable ref="tableRef" :columns="columns" :query-params="queryData" :apiFetch="postSysDevicePage">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'avatar'">
              <a-image :height="50" :width="50" :src="record.avatar" />
            </template>
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">编辑</a>
                <a class="btn" @click="remove(record.id)">删除</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="productdatabase-list" setup>
import {
  postSysDevicePage,
  postSysDeviceDelete,
  postSysDeviceImportData,
  postSysDeviceTemplateDown,
  postClientUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    username: "2121",
  }))
);
const queryOptions = ref([
  {
    name: "产品型号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "productType",
  },
  {
    name: "CODE",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "code",
  },

  {
    name: "更新时间",
    type: "range-picker",
    value: [],
    placeholder: "请选择",
    filed: "rangeDate",
    dateFields: ["startUpdateTime", "endUpdateTime"],
  },
]);
const columns = [
  {
    title: "产品型号",
    dataIndex: "productType",
    key: "productType",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "CODE",
    dataIndex: "code",
    key: "code",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "激活日期",
    dataIndex: "activeTime",
    key: "activeTime",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "更新时间",
    dataIndex: "updateTime",
    key: "updateTime",
    width: "200px",
    ellipsis: true,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const toDetail = (record) => {
  router.push({
    path: "/feedback/detail",
    query: {
      id: record?.id,
    },
  });
};

const edit = (record) => {
  router.push({
    path: "/productdatabase/edit",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  const title = `${record.status ? "启用" : "停用"}`;
  comfirmModal({
    content: `确认${title}该用户账号吗？`,
    onOk() {
      postClientUserUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
const remove = (id) => {
  comfirmModal({
    content: "确认删除该产品信息吗？",
    onOk() {
      postSysDeviceDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};
const add = () => { };
const importFile = () => {
  if (!tableRef.value.data?.length) {
    message.error("暂无数据");
    return;
  }
  const params = {};

  for (let key in queryData) {
    params[key] = queryData[key];
  }
  postSysDeviceImportData(params).then((res) => {
    message.success("导出成功");
  });
};
const importData = (data) => {
  if (data) {
    const { errorList, successList } = data;
    if (errorList?.length) {
      message.error(errorList[0].errMsg);
    }

    if (successList?.length) {
      message.success("导入成功");
      tableRef.value.refreshTableData();
    }
  }
};
const downFile = () => {
  postSysDeviceTemplateDown()
}
</script>

<style lang="less" scoped></style>
